package com.ws.util;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory;

public class DigestUtil {
	
	private final static int  countIndex = 12;
	private final static int saltIndex = 1;
	private final static char[] items = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
	
	
	public static String randomDigest(String password) {
		int randomCountIndex = (int)(Math.random()*items.length);
		int randomSaltIndex = (int)(Math.random()*items.length);
		char count = items[randomCountIndex];
		System.out.println(count);
		char salt = items[randomSaltIndex];
		System.out.println(salt);
		String result = password + salt;
		for(int i = 0 ; i < count;i++) {
			result = DigestUtils.md5Hex(result);
		}
		
		result = result.substring(0,saltIndex) + salt + result.substring(saltIndex);
		result = result.substring(0,countIndex) + count + result.substring(countIndex);
		System.out.println(result);
		return result;
	}
	
	
	public static boolean decode(String pass,String digestpass) {
		char salt = digestpass.charAt(saltIndex);
		char count = digestpass.charAt(countIndex);
		//开始加密
		String result = pass + salt;
		for(int i = 0 ;i < count;i++) {
			result = DigestUtils.md5Hex(result);
		}
		
		result = result.substring(0,saltIndex) + salt + result.substring(saltIndex);
		result = result.substring(0,countIndex) + count + result.substring(countIndex);
		System.out.println("真实密码" + digestpass);
		System.out.println( "输入的密码" + result);
		return result.equals(digestpass);
	}
	
	public static void main(String[] args) {
		System.out.println( "---"+randomDigest("123123"));
	}
}
